package com.itheima.web;

import com.itheima.domain.Product;
import com.itheima.service.ProductService;
import com.itheima.service.impl.ProductServiceImpl;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ProductServlet {
    private ProductService productService = new ProductServiceImpl();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
    List<Product> list =new ArrayList<>();
    Product product =new Product();
    Integer id;
    Integer result;

    /**
     * @throws Exception
     * 查询所有
     */
    @Test
    public void findAll(){
        list= productService.findAll();
        for (Product product1 : list) {
            System.out.println(product1);
        }
    }

    /**
     * @param id
     * 根据id查询单个
     */
    @Test
    public void findById(){
        id=3;
        product= productService.findById(id);
        System.out.println(product);
//        return product;
    }

    /**
     * @param pro
     * 根据id添加单个
     */
    @Test
    public void add(){
       product= new Product("iPad",1999.0,sdf.format(new Date()),"数码产品");

        result= productService.add(product);
        System.out.println(result);
      //  return result;
    }

    /**
     * 根据id修改
     */
    @Test
    public void updateById(){
        product.setId(3);
        product.setName("Lv");
        product.setPrice(13888.0);
        product.setCategory("鞋靴箱包");
        result= productService.updateById(product);
        System.out.println(result);
        //return result;
    }

    /**
     * 根据id删除 物理删除，恢复不了
     */
    @Test
    public void deleteById(){
        id=3;
       result= productService.deleteById(id);
        System.out.println(result);
        //return result;
    }

    /**
     * 根据id删除  状态删除
     */
    @Test
    public void deleteByIdStatus(){
        id=2;
        result= productService.deleteByIdStatus(id);
        System.out.println(result);
       // return result;
    }

    /**
     * @param pro
     * 通过姓名、入库时间和价格任意条件查询
     */
    @Test
    public void findByNPT(){
        product.setName("oppo手机");
        // product.setPrice(2999.0);
        // product.setInsertTime("2020-07-01" );
        list= productService.findByNPT(product);
        for (Product product1 : list) {
            System.out.println(product1);
        }
       // return list;
    }

    /**
     * @param pro
     * 修改指定列 name price
     */
    @Test
    public void updateByNameAndPrice(){
        product.setId(6);
        product.setName("oppo手机");
      //  product.setPrice(3499.0);
        result= productService.updateCon(product);
        System.out.println(result);
      //  return result;
    }

    /**
     * @param list
     * 批量增加
     */
    @Test
    public void addBatch(){
       // list.add(new Product(null,"oppo手机",3888.0,"2020-05-30" ,"数码产品"));
       // list.add(new Product(null,"魅族",2888.0,"2020-06-16" ,"数码产品"));
       // list.add(new Product(null,"小米电脑",6888.0,"2020-06-26" ,"数码产品"));
        for (int i = 0; i <100 ; i++) {
            list.add(new Product(null,"华为手机"+i,5999.0+i,sdf.format(new Date()),"数码产品"));
        }
        result= productService.addBatch(list);
        System.out.println(result);
     //   return result;
    }
}
